-module(d17).
-author("kk").

%% API
-export([getRepeat/2,getListRepeat/1]).

%% 用列表输出在列表或元组中查找到的的所有重复元素 [1,2,3,2]
getRepeat([]) -> [].
getRepeat(List, X) -> getRepeat(List, X, []).
getRepeat([], X, Result) ->
  Length = len(Result),
  if
    Length > 1 -> X;
    true -> []
  end;
getRepeat([H | List], X, Result) ->
  if
    H == X -> Temp = [X | Result], getRepeat(List, X, Temp);
    true -> getRepeat(List, X, Result)
  end.

%% 求列表长度
len([]) -> 0;
len([_ | List]) -> 1 + len(List).

getListRepeat(List) ->
  Length = len(List),
  getListRepeat(List, List, Length, 1, []).
getListRepeat([H | List], Origin, Length, Index, Result) ->
  if
    Index =< Length -> Get = getRepeat(Origin, H),
      if
        Get /= [] -> Temp = [Get | Result],
          getListRepeat(List, Origin, Length, Index + 1, Temp);
        true -> getListRepeat(List, Origin, Length, Index + 1, Result)
      end
  end;
getListRepeat([], Origin, Length, Index, Result) -> Result.
